package com.ershi.spring.aop.proxy.chain;

import com.ershi.spring.aop.proxy.common.Animal;
import com.ershi.spring.aop.proxy.common.Cat;
import com.ershi.spring.aop.proxy.common.TargetMethod;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


/**
 * @author 二十
 * @since 2021/10/31 10:07 下午
 */
public class Main {

	public static void main(String[] args) {

		AbstractSlot.Head head = new AbstractSlot.Head();
		AbstractSlot first = new First();
		AbstractSlot second=new Second();
		first.setNext(second);
		head.setNext(first);

		JdkDynamic jdkDynamic = new JdkDynamic(new Cat(),head);
		Animal proxy = (Animal) jdkDynamic.getProxy();
		proxy.eat("事物");

	}



	private static class First extends AbstractSlot{

		@Override
		Object invoke(TargetMethod targetMethod) {

			Object result = null;
			System.out.println("增强逻辑");
			try {
				result= proceed(targetMethod);
			} catch (Exception e) {
				e.printStackTrace();
			}
			System.out.println("增强逻辑");
			return result;
		}
	}

	private static class Second extends AbstractSlot{

		@Override
		Object invoke(TargetMethod targetMethod) {

			Object result = null;
			System.out.println("增强逻辑");
			try {
				result= proceed(targetMethod);
			} catch (Exception e) {
				e.printStackTrace();
			}
			System.out.println("增强逻辑");
			return result;
		}
	}
}
